﻿package mortal.game.view.guild.buildingPanel
{
    import Message.DB.Tables.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.guild.cellrender.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildShopPanel extends BaseWindow
    {
        private const showAmount:int = 6;
        private var _tabData:Array;
        private var _tabBar:GTabarNew;
        private var _pageSelecter:PageSelecter;
        private var _goodsList:GTileList;
        private var _totoalContribution:GTextFiled;
        private static var _instance:GuildShopPanel;

        public function GuildShopPanel(param1:ILayer = null)
        {
            super(param1);
            this.setSize(335, 500);
            this.title = "" + Language.getString(71068);
            if (_instance)
            {
                throw new Error("GuildWareHousePanel single");
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.bg(8, 55, 324, 407, this, ImagesConst.PanelBg));
            this.pushUIToDisposeVec(UIFactory.bg(8, 466, 324, 27, this, ImagesConst.PanelBg));
            this._tabData = GuildShopConfig.instance.getShopTabByLevel(Cache.instance.guild.selfGuildInfo.baseInfo.level);
            this._tabBar = UIFactory.gTabBarNew(2, 32, this._tabData, 335, 500, 65, 26, this, this.tabBarChangeHandler, "TabButtonNew");
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30091), 10, 470, 100, 20, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField("" + Language.getString(71069), 225, 470, 120, 20, this, GlobalStyle.textFormatHuang));
            this._goodsList = UIFactory.tileList(15, 63, 335, 400, this);
            this._goodsList.rowHeight = 55;
            this._goodsList.columnWidth = 335;
            this._goodsList.horizontalGap = 5;
            this._goodsList.verticalGap = 5;
            this._goodsList.setStyle("cellRenderer", GuildShopCellRenderer);
            this._goodsList.isClickedUpdate = false;
            this._totoalContribution = UIFactory.gTextField("", 40, 470, 60, 20, this, GlobalStyle.textFormatHuang.left());
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.align = AlignMode.CENTER;
            this._pageSelecter = UIFactory.pageSelecter(100, 438, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, _loc_1);
            this._pageSelecter.pageTextBoxSize = 50;
            this._pageSelecter.currentPage = 1;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this.updateContribution();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._tabBar.dispose(param1);
            this._tabBar = null;
            this._goodsList.dispose(param1);
            this._goodsList = null;
            this._totoalContribution.dispose(param1);
            this._totoalContribution = null;
            this._pageSelecter.dispose(param1);
            this._pageSelecter = null;
            return;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            return;
        }// end function

        private function onPageChange(event:Event = null) : void
        {
            this._goodsList.dataProvider = this.getDataProvider();
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:TGuildShopSell = null;
            var _loc_6:Object = null;
            var _loc_7:int = 0;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.guild.selfGuildInfo.shop.itemsArr;
            this._pageSelecter.maxPage = Math.ceil(_loc_2.length / this.showAmount);
            if (_loc_2)
            {
                _loc_3 = this.showAmount + (this._pageSelecter.currentPage - 1) * this.showAmount;
                _loc_4 = (this._pageSelecter.currentPage - 1) * this.showAmount;
                _loc_7 = _loc_4;
                while (_loc_7 < _loc_3)
                {
                    
                    if (_loc_2[_loc_7])
                    {
                        _loc_5 = GuildShopConfig.instance.getGuildShopSellByCode(_loc_2[_loc_7].itemCode);
                        if (_loc_5)
                        {
                            _loc_6 = {sguildShopItem:_loc_2[_loc_7], tGuildShopItem:_loc_5};
                            _loc_1.addItem(_loc_6);
                        }
                    }
                    _loc_7++;
                }
            }
            return _loc_1;
        }// end function

        public function updateShopItem() : void
        {
            this._goodsList.dataProvider = this.getDataProvider();
            return;
        }// end function

        public function updateContribution(param1:Object = null) : void
        {
            this._totoalContribution.text = Cache.instance.guild.selfGuildInfo.selfInfo.contribution.toString();
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            NetDispatcher.addCmdListener(ServerCommand.GUILD_INFO_UPDATE, this.updateContribution);
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            NetDispatcher.removeCmdListener(ServerCommand.GUILD_INFO_UPDATE, this.updateContribution);
            return;
        }// end function

        public static function get instance() : GuildShopPanel
        {
            if (!_instance)
            {
                _instance = new GuildShopPanel;
            }
            return _instance;
        }// end function

    }
}
